// ===============================================
// smart_refresher_by_list
// 拓展下拉刷新的组件 封装他的一意义是在于
// 采用统一 的下俩顶部  或者上拉的底部UI
// 采用分帧技术
// 还可以拓展 网络 不通的时候 有那个 网络提示
// Create by Will on 14/9/2023 15:59
// Copyright Will .All rights reserved.
// ===============================================

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keframe/keframe.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'page_status.dart';
import 'refresh_indicator.dart';

class SmartRefresherByList extends StatelessWidget {
  const SmartRefresherByList({
    required this.controller,
    required this.child,
    required this.onRefresh,
    this.itmList = 0,
    this.onLoading,
    this.refreshHeader,
    this.footer,
    this.footerHeight,
    this.enablePullDown = true,
    this.enablePullUp = true,
    this.placeHolder,
    this.padding,
    //如果不是web 采用分帧
    this.useFraming = !kIsWeb,
    this.physics = const ClampingScrollPhysics(),
    this.cacheExtent = 500,
    this.scrollController,
    this.isSeparated = false,
    this.separatorBuilder,
  });

  //刷新控件
  final RefreshController controller;

  //下拉刷新的方法
  final VoidCallback onRefresh;

  //上拉加载的方法
  final VoidCallback? onLoading;

  //里面放的组件
  final NullableIndexedWidgetBuilder child;

  //头部 一般不需要改
  final Widget? refreshHeader;

  //脚步 一般不需要改
  final Widget? footer;

  final double? footerHeight;

  //能否上拉
  final bool enablePullUp;

  //能否下来
  final bool enablePullDown;

  //返回的 个数
  final int itmList;

  //这个就是放占位图片的
  final Widget? placeHolder;

  //间距的问题
  final EdgeInsetsGeometry? padding;

  //是否需要 分帧技术
  final bool useFraming;

  final ScrollPhysics? physics;

  //列表预加载的高度
  final double cacheExtent;

  final ScrollController? scrollController;

  final bool isSeparated;

  final IndexedWidgetBuilder? separatorBuilder;

  @override
  Widget build(BuildContext context) {
    final body = SmartRefresher(
      enablePullUp: enablePullUp,
      enablePullDown: enablePullDown,
      // header: refreshHeader ?? const RefreshHeader(),
      header: refreshHeader ?? const MaterialClassicHeader(),
      footer: footer ?? RefreshFooter(footerHeight: footerHeight),
      controller: controller,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: _buildScrollView(context: context),
    );
    return useFraming ? SizeCacheWidget(child: body) : body;
  }

  Widget _buildScrollView({required BuildContext context}) {
    if (itmList == 0) {
      if (controller.headerMode?.value == RefreshStatus.completed) {
        return PageStatus.noData();
      } else {
        return Center(
          child: CircularProgressIndicator(
            color: Theme.of(context).primaryColor,
          ),
        );
      }
    }
    return isSeparated
        ? ListView.separated(
            padding: padding,
            cacheExtent: cacheExtent,
            itemCount: itmList,
            physics: physics,
            controller: scrollController,
            itemBuilder: (c, i) => useFraming
                ? FrameSeparateWidget(
                    index: i,
                    // placeHolder: placeHolder ??
                    //     Container(
                    //       color: Theme.of(context).scaffoldBackgroundColor,
                    //       height: 68,
                    //     ),
                    child: child(c, i)!,
                  )
                : child(c, i)!,
            separatorBuilder: separatorBuilder!,
          )
        : ListView.builder(
            padding: padding,
            cacheExtent: cacheExtent,
            itemCount: itmList,
            physics: physics,
            controller: scrollController,
            itemBuilder: (c, i) => useFraming
                ? FrameSeparateWidget(
                    index: i,
                    // placeHolder: placeHolder ??
                    //     Container(
                    //       color: Theme.of(context).scaffoldBackgroundColor,
                    //       height: 68,
                    //     ),
                    child: child(c, i)!,
                  )
                : child(c, i)!,
          );
  }
}
